﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;

namespace Cybozu.SharePoint.CategoryFilter
{
    public class CategoryFilterViewPage : WebPartPage
    {
        protected Panel LeftPanel;
        protected WebPartZone Main;

        protected void Page_Init()
        {
            SPWeb web = SPContext.Current.Web;
            SPList splist = SPContext.Current.List;
            SPView view = SPContext.Current.ViewContext.View;

            BulletedList categoryList = new BulletedList();
            categoryList.DisplayMode = BulletedListDisplayMode.HyperLink;
            categoryList.BulletStyle = BulletStyle.CustomImage;
            categoryList.BulletImageUrl = "/_layouts/images/folder.gif";
            LeftPanel.Controls.Add(categoryList);

            SPField categoryField = CategoryFilterSettingsPage.GetCategoryField(splist);
            if (categoryField != null)
            {
                categoryList.Items.Add(new ListItem(Utility.GetLocalizedString(Resource.fld_all, Resource.core, web), view.ServerRelativeUrl));

                string baseUrl = string.Format("{0}?View={1}&FilterField1={2}&FilterValue1=", view.ServerRelativeUrl, SPHttpUtility.UrlKeyValueEncode(view.ID), SPHttpUtility.UrlKeyValueEncode(categoryField.InternalName));
                categoryList.Items.Add(new ListItem(Utility.GetLocalizedString(Resource.fld_empty, Resource.core, web), baseUrl));

                if (categoryField.Type == SPFieldType.Choice || categoryField.Type == SPFieldType.MultiChoice)
                {
                    SPFieldChoice choiceField = categoryField as SPFieldChoice;
                    foreach (string choice in choiceField.Choices)
                    {
                        ListItem menuItem = new ListItem(choice, baseUrl);
                        menuItem.Value += SPHttpUtility.UrlKeyValueEncode(choice);
                        categoryList.Items.Add(menuItem);
                    }
                }
                else if (categoryField.Type == SPFieldType.Lookup)
                {
                    SPFieldLookup lookupField = categoryField as SPFieldLookup;
                    try
                    {
                        SPList lookupList = web.Lists[new Guid(lookupField.LookupList)];
                        SPQuery query = new SPQuery();
                        query.RowLimit = view.RowLimit;
                        query.Query = "<OrderBy><FieldRef Name='Title' Ascending='TRUE' /></OrderBy>";
                        SPListItemCollection items = lookupList.GetItems(query);
                        foreach (SPListItem item in items)
                        {
                            ListItem menuItem = new ListItem(item.Title, baseUrl);
                            menuItem.Value += SPHttpUtility.UrlKeyValueEncode(item.Title);
                            categoryList.Items.Add(menuItem);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        public XsltListViewWebPart ViewWebPart
        {
            get
            {
                foreach (WebPart webpart in Main.WebParts)
                {
                    if (webpart is XsltListViewWebPart) return webpart as XsltListViewWebPart;
                }

                return null;
            }
        }
    }
}
